package org.bluesky.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.bluesky.framework.ResponseResult;
import org.bluesky.framework.constants.SystemConstants;
import org.bluesky.framework.entity.Comment;
import org.bluesky.framework.enums.AppHttpCodeEnum;
import org.bluesky.framework.exception.SystemException;
import org.bluesky.framework.mapper.CommentMapper;
import org.bluesky.framework.service.CommentService;
import org.bluesky.framework.service.UserService;
import org.bluesky.framework.utils.BeanCopyUtils;
import org.bluesky.framework.vo.CommentVo;
import org.bluesky.framework.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2023-02-16 14:09:47
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private UserService userService;

    @Override
    public ResponseResult commentList(String commentType, Long articleId, Integer pageNum, Integer pageSize) {
        //查询对应文章的根评论
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        //articleId进行判断
        queryWrapper.eq(SystemConstants.ARTICLE_COMMENT.equals(commentType),Comment::getArticleId,articleId);
        //rootId == -1
        queryWrapper.eq(Comment::getRootId, SystemConstants.IS_ROOT_COMMENT);
        //评论类型判断
        queryWrapper.eq(Comment::getType,commentType);
        // 分页查询
        Page<Comment> page = new Page<>(pageNum,pageSize);
        page(page, queryWrapper);
        List<CommentVo> vos = toCommentVoList(page.getRecords());
        //查询所有根评论和对应子评论集合，并赋值给对应的属性
        for (CommentVo commentVo : vos) {
            //查询对应的子评论
            List<CommentVo> children = getChildren(commentVo.getId());
            commentVo.setChildren(children);
        }
        return ResponseResult.okResult(new PageVo(vos,page.getTotal()));
    }

    /**
     * 赋值username和toCommentUserName
     * @param commentList
     * @return
     */
    private List<CommentVo> toCommentVoList(List<Comment> commentList){
        List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(commentList, CommentVo.class);
        //遍历vo集合
        //通过createdBy查询用户名称并赋值
        // 通过toCommentUserId查询用户名并赋值
        List<CommentVo> comments = commentVos.stream()
                .peek(commentVo -> {
                    commentVo.setUsername(userService.getById(commentVo.getCreateBy()).getNickName());
                    //通过toCommentId查询用户的昵称并赋值
                    //如果toCommentUserId不为-1才进行查询
                    if (commentVo.getToCommentUserId() != -1) {
                        commentVo.setToCommentUserName(userService.getById(commentVo.getToCommentUserId()).getNickName());
                    }
                }).collect(Collectors.toList());
        return comments;
    }

    /**
     * 根据根评论的id查询对应子评论
     * @param id
     * @return
     */
    private List<CommentVo> getChildren(Long id){
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getRootId,id);
//        queryWrapper.orderByDesc(Comment::getCreateTime);
        queryWrapper.orderByAsc(Comment::getCreateTime);
        List<Comment> list = list(queryWrapper);
        return toCommentVoList(list);
    }

    @Override
    public ResponseResult addComment(Comment comment) {
        if(!StringUtils.hasText(comment.getContent())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        save(comment);
        return ResponseResult.okResult();
    }
}

